/*     */ package com.wgcloud.util.jdbc;
/*     */ 
/*     */ import cn.hutool.core.collection.CollectionUtil;
/*     */ import com.wgcloud.entity.DbInfo;
/*     */ import com.wgcloud.entity.DbTable;
/*     */ import com.wgcloud.service.DbInfoService;
/*     */ import com.wgcloud.service.LogInfoService;
/*     */ import com.wgcloud.util.FormatUtil;
/*     */ import com.wgcloud.util.ThreadPoolUtil;
/*     */ import com.wgcloud.util.msg.WarnMailUtil;
/*     */ import com.wgcloud.util.msg.WarnPools;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.annotation.Resource;
/*     */ import javax.sql.DataSource;
/*     */ import org.apache.commons.lang3.StringUtils;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.jdbc.core.JdbcTemplate;
/*     */ import org.springframework.jdbc.datasource.DriverManagerDataSource;
/*     */ import org.springframework.stereotype.Component;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ @Component
/*     */ public class ConnectionUtil
/*     */ {
/*  34 */   private static final Logger logger = LoggerFactory.getLogger(com.wgcloud.util.jdbc.ConnectionUtil.class);
/*     */   @Resource
/*     */   private LogInfoService logInfoService;
/*     */   @Resource
/*     */   private DbInfoService dbInfoService;
/*     */   
/*     */   public JdbcTemplate getJdbcTemplate(DbInfo dbInfo) throws Exception {
/*  41 */     JdbcTemplate jdbcTemplate = null;
/*  42 */     String driver = "";
/*  43 */     String url = dbInfo.getDbUrl();
/*  44 */     if ("mysql".equals(dbInfo.getDbType())) {
/*  45 */       driver = "com.mysql.jdbc.Driver";
/*  46 */     } else if ("mariadb".equals(dbInfo.getDbType())) {
/*  47 */       driver = "org.mariadb.jdbc.Driver";
/*  48 */     } else if ("postgresql".equals(dbInfo.getDbType())) {
/*  49 */       driver = "org.postgresql.Driver";
/*  50 */     } else if ("sqlserver".equals(dbInfo.getDbType())) {
/*  51 */       driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
/*  52 */     } else if ("db2".equals(dbInfo.getDbType())) {
/*  53 */       driver = "com.ibm.db2.jdbc.app.DB2Driver";
/*     */     } else {
/*  55 */       driver = "oracle.jdbc.driver.OracleDriver";
/*     */     } 
/*     */     
/*     */     try {
/*  59 */       DriverManagerDataSource dataSource = new DriverManagerDataSource();
/*  60 */       dataSource.setDriverClassName(driver);
/*  61 */       dataSource.setUrl(url);
/*  62 */       dataSource.setUsername(dbInfo.getUser());
/*  63 */       dataSource.setPassword(dbInfo.getPasswd());
/*  64 */       jdbcTemplate = new JdbcTemplate((DataSource)dataSource);
/*  65 */       jdbcTemplate.setQueryTimeout(60);
/*  66 */       if ("mysql".equals(dbInfo.getDbType())) {
/*  67 */         jdbcTemplate.queryForRowSet("select version()");
/*  68 */       } else if ("mariadb".equals(dbInfo.getDbType())) {
/*  69 */         jdbcTemplate.queryForRowSet("select version()");
/*  70 */       } else if ("postgresql".equals(dbInfo.getDbType())) {
/*  71 */         jdbcTemplate.queryForRowSet("select version()");
/*  72 */       } else if ("sqlserver".equals(dbInfo.getDbType())) {
/*  73 */         jdbcTemplate.queryForRowSet("SELECT @@VERSION");
/*  74 */       } else if ("db2".equals(dbInfo.getDbType())) {
/*  75 */         jdbcTemplate.queryForRowSet("SELECT SERVICE_LEVEL FROM SYSIBMADM.ENV_INST_INFO");
/*     */       } else {
/*  77 */         jdbcTemplate.queryForRowSet("select * from v$version");
/*     */       } 
/*  79 */       dbInfo.setDbState("1");
/*  80 */       if (!StringUtils.isEmpty((CharSequence)WarnPools.MEM_WARN_MAP.get(dbInfo.getId()))) {
/*  81 */         Runnable runnable = () -> WarnMailUtil.sendDbDown(dbInfo, false);
/*     */ 
/*     */         
/*  84 */         ThreadPoolUtil.executor.execute(runnable);
/*     */       } 
/*  86 */       this.dbInfoService.updateById(dbInfo);
/*  87 */       return jdbcTemplate;
/*  88 */     } catch (Exception e) {
/*  89 */       jdbcTemplate = null;
/*  90 */       logger.error("连接数据库错误", e);
/*  91 */       dbInfo.setDbState("2");
/*  92 */       this.dbInfoService.updateById(dbInfo);
/*  93 */       Runnable runnable = () -> WarnMailUtil.sendDbDown(dbInfo, true);
/*     */ 
/*     */       
/*  96 */       ThreadPoolUtil.executor.execute(runnable);
/*     */       
/*  98 */       return null;
/*     */     } 
/*     */   }
/*     */   public long queryTableCount(DbInfo dbInfo, DbTable dbTable) {
/*     */     try {
/* 103 */       String sqlinkey = FormatUtil.haveSqlDanger(dbTable.getWhereVal());
/* 104 */       if (!StringUtils.isEmpty(sqlinkey)) {
/* 105 */         String errinfo = "统计SQL语句含有sql敏感字符" + sqlinkey;
/* 106 */         logger.error("统计数据表错误：" + errinfo);
/* 107 */         this.logInfoService.save(errinfo + "：" + dbInfo.getAliasName(), "数据库别名：" + dbInfo.getAliasName() + "，错误信息：" + errinfo, "1");
/* 108 */         return 0L;
/*     */       } 
/*     */       
/* 111 */       JdbcTemplate jdbcTemplate = getJdbcTemplate(dbInfo);
/* 112 */       if (null == jdbcTemplate) {
/* 113 */         return 0L;
/*     */       }
/* 115 */       if (StringUtils.isEmpty(dbTable.getTableName())) {
/* 116 */         return ((Long)jdbcTemplate.queryForObject(dbTable.getWhereVal(), Long.class)).longValue();
/*     */       }
/* 118 */       List<Map<String, Object>> list = jdbcTemplate.queryForList(dbTable.getWhereVal());
/* 119 */       if (CollectionUtil.isEmpty(list)) {
/* 120 */         return 0L;
/*     */       }
/* 122 */       Map<String, Object> mapResult = list.get(0);
/* 123 */       return Long.valueOf(mapResult.get(dbTable.getTableName()).toString()).longValue();
/*     */     
/*     */     }
/* 126 */     catch (Exception e) {
/* 127 */       logger.error("统计数据表错误：", e);
/* 128 */       this.logInfoService.save("统计数据表sql执行错误：" + dbInfo.getAliasName(), "数据库别名：" + dbInfo.getAliasName() + "，错误信息：" + e.toString(), "1");
/* 129 */       return 0L;
/*     */     } 
/*     */   }
/*     */ }


/* Location:              /Users/zhangyongxin/soft-install/wgcloud/wgcloud-v3.3.5/server/bank/wgcloud-server-release.jar!/BOOT-INF/classes/com/wgcloud/util/jdbc/ConnectionUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */